package com.chenyue.cm.live.controller;


import com.chenyue.cm.base.aop.Log;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.bean.Pageable;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.RequestUtil;
import com.chenyue.cm.live.domain.JdLive;
import com.chenyue.cm.live.domain.JdLiveApply;
import com.chenyue.cm.live.domain.JdLiveApplyExample;
import com.chenyue.cm.live.service.JdLiveApplyService;
import com.chenyue.cm.live.service.JdLiveService;
import com.chenyue.cm.live.vo.LiveApplyVerify;
import com.chenyue.cm.user.domain.JdVerify;
import com.chenyue.cm.user.service.JdAppUserService;
import com.chenyue.cm.user.service.JdVerifyService;
import com.chenyue.cm.utils.DateUtils;
import com.chenyue.cm.utils.PageInfoUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenyue-cm
 * @date 2021-07-20
 */
@RestController
@RequestMapping("/jdLiveApply")
public class JdLiveApplyController {

    private final JdLiveApplyService jdLiveApplyService;

    private final JdVerifyService verifyService;

    private final JdLiveService jdLiveService;

    private final JdAppUserService appUserService;

    public JdLiveApplyController(JdLiveApplyService jdLiveApplyService, JdVerifyService verifyService, JdLiveService jdLiveService, JdAppUserService appUserService) {
        this.jdLiveApplyService = jdLiveApplyService;
        this.verifyService = verifyService;
        this.jdLiveService = jdLiveService;
        this.appUserService = appUserService;
    }

    @RequestMapping("/getList")
    public Success getList(HttpServletRequest request) {
        Pageable pageInfo = RequestUtil.getPageable(request);
        PageVo<JdLiveApply> result = jdLiveApplyService.getPageBean(pageInfo, JdLiveApply.class);
        return Success.success(result);
    }

    /**
     * @param vo 插入实体
     * @return Success
     */
    @Log
    @RequestMapping("/save")
    public Success save(JdLiveApply vo) {
        boolean success = jdLiveApplyService.save(vo);
        return success ? Success.success() : Success.error();
    }

    /**
     * @param vo      修改实体
     * @param request id
     * @return Success
     */
    @Log
    @RequestMapping("/edit")
    public Success edit(JdLiveApply vo, HttpServletRequest request) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)) {
            int success = jdLiveApplyService.getBaseMapper().updateById(vo);
            return success > 0 ? Success.success() : Success.error();
        } else {
            return Success.error("id不能为空");
        }
    }

    /**
     * @param id 主键
     * @return Success
     */
    @Log
    @RequestMapping("/delete/{id}")
    public Success deleteByPrimaryKey(@PathVariable("id") Integer id) {
        JdLiveApplyExample aExample = new JdLiveApplyExample();
        aExample.createCriteria().andIdEqualTo(id);
        int success = jdLiveApplyService.deleteByExample(aExample);
        return success > 0 ? Success.success() : Success.error();
    }

    /**
     * 【API】----  删除多个数据
     *
     * @return Success
     */
    @Log
    @RequestMapping("/delete")
    public Success delete(String ids) {
        String[] idStrings = ids.split(",");
        for (String idString : idStrings) {
            int success = jdLiveApplyService.deleteByPrimaryKey(idString);
            if (success == 0) {
                return Success.error();
            }
        }
        return Success.success();
    }

    @Log
    @RequestMapping("/applyLive")
    public Success applyLive(HttpServletRequest request) {
        String id = request.getParameter("id");
        String state = request.getParameter("state");
        String remark = request.getParameter("remark");
        JdLiveApply liveApply = jdLiveApplyService.getById(id);
        //校验该用户已经是主播
        if("1".equals(state)){
            final JdLive jdLive = jdLiveService.lambdaQuery().eq(JdLive::getUserId, liveApply.getUserId()).last(" limit 1 ").one();
            if (jdLive != null) {
                return Success.error("该用户已经是主播啦,不可以再申请啦");
            }
        }
        liveApply.setState(Integer.parseInt(state));
        liveApply.setRemark(remark);
        liveApply.setEt(DateUtils.getToday());
        liveApply.setEu("0");
        jdLiveApplyService.updateByPrimaryKey(liveApply);
        if ("1".equals(state)) {
            JdLive live = new JdLive();
            live.setUserId(liveApply.getUserId());
            live.setCt(DateUtils.getToday());
            live.setType(1);
            live.setLiveNum(appUserService.getById(liveApply.getUserId()).getUserNum());
            jdLiveService.save(live);
        }
        return Success.success();
    }

    @RequestMapping("/getLiveApply")
    public Success getLiveApply(HttpServletRequest request) {
        List<LiveApplyVerify> list = new ArrayList<>();

        Pageable pageInfo = RequestUtil.getPageable(request);
        //设置分页信息
        PageHelper.startPage(pageInfo.getPageIndex(), pageInfo.getPageSize(), true);
        List<JdLiveApply> liveApplyList = jdLiveApplyService.list();
        for (JdLiveApply apply : liveApplyList) {
            //实名信息
            JdVerify verifyInfo = verifyService.getVerifyInfo(apply.getUserId());
            LiveApplyVerify vo = new LiveApplyVerify();
            vo.setId(apply.getId());
            vo.setUserId(apply.getUserId());
            vo.setUserName(verifyInfo.getName());
            vo.setCardNumber(verifyInfo.getCardNumber());
            vo.setCertPositive(verifyInfo.getCertPositive());
            vo.setCertReverse(verifyInfo.getCertReverse());
            vo.setState(apply.getState());
            vo.setRemark(apply.getRemark());
            vo.setCt(apply.getCt());
            vo.setEt(apply.getEt());
            vo.setEu(apply.getEu());
            list.add(vo);
        }
        //封装返回结果
        PageInfo<JdLiveApply> resultSource = new PageInfo<>(liveApplyList);
        PageInfo<LiveApplyVerify> result = new PageInfo<>(list);
        PageInfoUtils<JdLiveApply, LiveApplyVerify> tgObj = new PageInfoUtils<>();
        tgObj.convertPageInfo(resultSource,result);
        return Success.success(result);
    }

}
